import pandas as p
import matplotlib.pyplot as plt
from collections import OrderedDict
import numpy as np
import seaborn as sns
import sys
#Plot a tick in the middle of the plot rather than along an axis
def plotTick(ax, xLoc, round=0):
    height = .05
    zorder = 1
    if(round==0):
        ax.plot([int(xLoc), int(xLoc)], [height, -height], color='xkcd:black', zorder=zorder)
        ax.text(int(xLoc), -height*1.5, str(int(xLoc)), ha='center', va='top', zorder=zorder)
    else:
        ax.plot([np.round(xLoc, decimals=round), np.round(xLoc, decimals=round)], [height, -height], color='xkcd:black', zorder=zorder)
        ax.text(np.round(xLoc, decimals=round), -height * 1.5, str(np.round(xLoc, decimals=round)), ha='center', va='top', zorder=zorder)


#Plot multiple ticks
def plotTicks(ax, xLocs, round=0):
    xlim = plt.xlim()
    minXLim = min(list(xlim))
    maxXlim = max(list(xlim))
    for xLoc in xLocs:
        if(xLoc>minXLim and xLoc<maxXlim):
            plotTick(ax, xLoc, round)

#Plot a bracket
def plotBracket(xs, message, top=True, single=False):
    loX = min(xs)
    hiX = max(xs)

    if(top):
        loY = 1.43
        midY = 1.48
        hiY = 1.53
        textY = 1.6
    else:
        if(single):
            loY = .57
            midY = .52
            hiY = .47
            textY = .15
        else:
            loY = .57
            midY = 0
            hiY = -.05
            textY = -.2


    plt.plot((loX, loX), (loY, midY), color='xkcd:black')
    plt.plot((hiX, hiX), (loY, midY), color='xkcd:black')
    plt.plot((loX, hiX), (midY, midY), color='xkcd:black')
    plt.plot(((loX+hiX)/2, (loX+hiX)/2), (midY, hiY), color='xkcd:black')
    plt.text((loX+hiX)/2, textY, message, ha='center', size=14)

#Collect full externality values
def getExternalityDict(full=False):

    if(sys.platform=='darwin'):
        file = 'PythonScripts/Tables/externalities.xlsx'
        file = 'PythonScripts/Tables/externalities.xlsx'
    else:
        file = r'PythonScripts\Tables\externalities.xlsx'
        file = r'PythonScripts\Tables\externalities.xlsx'

    if(full):
        extKeys = ['Elast. Lower Bound', 'Est. Elast.', 'Elast.=0', 'Elast. Upper Bound', 'Jacobsen', 'PRIA', 'FRIA', 'PRIA-Star', 'FRIA-Star']
        values = []
        col = 'Total Externalities (2021$M)'
        for i in range(1, len(extKeys)+1):
            tempSheet = 'Externalities{}'.format(i)
            tempData = p.read_excel(
                file,
                tempSheet)
            tempData = tempData.loc[tempData.loc[:, 'State'].str.contains("Total"), :]
            values.append(float(tempData[col]))
    else:
        extKeys = ['Est. Elast.', 'Elast.=0', 'Elast. Upper Bound', 'Jacobsen', 'PRIA']
        values = []
        col = 'Total Externalities (2021$M)'
        for i in range(2, 7):
            tempSheet = 'Externalities{}'.format(i)
            tempData = p.read_excel(
                file,
                tempSheet)
            tempData = tempData.loc[tempData.loc[:, 'State'].str.contains("Total"), :]
            values.append(float(tempData[col]))

    return OrderedDict(zip(extKeys, values))

#Get externality/policy impacts of travel and gas use
def getSubExternalityDict(full=False, vmt=True, externality = True):

    if(vmt):
        if(externality):
            col = 'VMT Externalities (2021$M)'
        else:
            col = 'Policy Impact on VMT'
    else:
        if(externality):
            col = 'Gas Use Externalities (2021$M)'
        else:
            col = 'Policy Impact on Gas Use'

    if (sys.platform == 'darwin'):
        file = 'PythonScripts/Tables/externalities.xlsx'
    else:
        file = r'PythonScripts\Tables\externalities.xlsx'

    if (full):
        extKeys = ['Elast. Lower Bound', 'Est. Elast.', 'Elast.=0', 'Elast. Upper Bound', 'Jacobsen', 'PRIA', 'FRIA', 'PRIA-Star', 'FRIA-Star']
        values = []

        for i in range(1, len(extKeys) + 1):
            tempSheet = 'Externalities{}'.format(i)
            tempData = p.read_excel(
                file,
                tempSheet)
            tempData = tempData.loc[tempData.loc[:, 'State'].str.contains("Total"), :]
            values.append(float(tempData[col]))

    else:
        extKeys = ['Est. Elast.', 'Elast.=0', 'Elast. Upper Bound', 'Jacobsen', 'PRIA']
        values = []
        for i in range(2, 7):
            tempSheet = 'Externalities{}'.format(i)
            tempData = p.read_excel(
                file,
                tempSheet)
            tempData = tempData.loc[tempData.loc[:, 'State'].str.contains("Total"), :]
            values.append(float(tempData[col]))


    return OrderedDict(zip(extKeys, values))


#Full Externality Plot
x = 6
gr = (1+np.sqrt(5))/2
y = x/(gr)

fig, (lax, ax) = plt.subplots(1,2,figsize=(x,y), sharey=True, gridspec_kw={'width_ratios':[.15, .85]})
fig, ax = plt.subplots(1,1,figsize=(x,y), sharey=True)

extDict = getExternalityDict(True)


offset = 3
nullColors = sns.color_palette('bright', n_colors=offset+9)[offset:]
jacobsenColor = nullColors[0]
priaColor = nullColors[1]
friaColor = nullColors[2]
priaStarColor = nullColors[3]
friaStarColor = nullColors[7]

supTitleHeight = 1
dpi = 300
bounds = [-900, 450]
dotSize = 75
lineWidth = 3
deltaBars = 20
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.text(extDict['Elast. Upper Bound'], 1/2+.1, '95% Confidence\nUpper Bound', ha='center', va='bottom', color='xkcd:green')
plt.text(extDict['Elast. Lower Bound'], 1/2+.1, '95% Confidence\nLower Bound', ha='left', va='bottom', color='xkcd:green')
plt.scatter([extDict['Est. Elast.'], extDict['Est. Elast.']], [0, 0], color='xkcd:dark green', s=dotSize + 20, zorder=3, marker='d')
plt.text(extDict['Est. Elast.'] - 10, .5 / 2, 'Estimated\nElasticities', ha='center', va='center', color='xkcd:dark green')
plt.scatter([extDict['Jacobsen'], extDict['Jacobsen']], [0, 0], color=jacobsenColor, s=dotSize, zorder=2)
plt.text(extDict['Jacobsen']+deltaBars, -.8 / 2, 'Unit\nElasticity', ha='center', va='center', color=jacobsenColor)
plt.scatter([extDict['PRIA'], extDict['PRIA']], [0, 0], color=priaColor, marker='o', s=dotSize, zorder=2)
plt.text(extDict['PRIA'], .4 / 2, 'SAFE\nPRIA', ha='center', va='center', color=priaColor)
plt.scatter([extDict['FRIA'], extDict['FRIA']], [0, 0], color=friaColor, marker='o', s=dotSize, zorder=3)
plt.text(extDict['FRIA'], .4 / 2, 'SAFE\nFRIA', ha='center', va='center', color=friaColor)

plt.scatter([extDict['PRIA-Star'], extDict['PRIA-Star']], [0, 0], color=priaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['PRIA-Star'], -.6 / 2, 'SAFE\nPRIA*', ha='center', va='center', color=priaStarColor, zorder=5)
plt.scatter([extDict['FRIA-Star'], extDict['FRIA-Star']], [0, 0], color=friaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['FRIA-Star']-50, .4 / 2, 'SAFE\nFRIA*', ha='center', va='center', color=friaStarColor, zorder=5)


plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

xlim = plt.xlim()
plt.plot(bounds, [0,0], color='xkcd:black', zorder=1)

plt.xlim(bounds)
leftXTicks = lax.get_xticks()
newLeftXTicks = leftXTicks[:len(leftXTicks)-1]
lax.set_xticks(newLeftXTicks)
rightTicks, rightLabels = plt.xticks()
plotTicks(ax, rightTicks)
# plotTicks(lax, newLeftXTicks)
ax.set_xticks([])
lax.set_xticks([])
plt.ylim([-1,1])

plt.yticks([])
plt.suptitle('Total Externalities (2021$M/State-Year)\nUnder Estimated Elasticities and Null Hypotheses', y=supTitleHeight)

xlim = plt.xlim()
ylim = plt.ylim()

plt.fill_between([min(xlim), 0], max(ylim), min(ylim), color='xkcd:gray', alpha=.25)
plt.ylim(ylim)
plt.xlim(xlim)


if(sys.platform=='darwin'):
    saveFile = 'PythonScripts/Plots/Externalities/TotalExternalities.png'
else:
    saveFile = r'PythonScripts\Plots\Externalities\TotalExternalities.png'
plt.savefig(saveFile, bbox_inches='tight', dpi=dpi)

#VMT Plot
fig, (lax, ax) = plt.subplots(1,2,figsize=(x,y), sharey=True, gridspec_kw={'width_ratios':[.15, .85]})
fig, ax = plt.subplots(1,1,figsize=(x,y), sharey=True)

extDict = getSubExternalityDict(True, True)

plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

plt.text(extDict['Elast. Upper Bound'], 1/2+.1, '95% Confidence\nUpper Bound', ha='center', va='bottom', color='xkcd:green')
plt.text(extDict['Elast. Lower Bound'], 1/2+.1, '95% Confidence\nLower Bound', ha='left', va='bottom', color='xkcd:green')
plt.scatter([extDict['Est. Elast.'], extDict['Est. Elast.']], [0, 0], color='xkcd:dark green', s=dotSize + 20, zorder=3, marker='d')
plt.text(extDict['Est. Elast.'] - 10, .5 / 2, 'Estimated\nElasticities', ha='center', va='center', color='xkcd:dark green')
plt.scatter([extDict['Jacobsen'], extDict['Jacobsen']], [0, 0], color=jacobsenColor, s=dotSize, zorder=2)
plt.text(extDict['Jacobsen']-deltaBars*1.5, -.6 / 2, 'Unit\nElasticity', ha='left', va='center', color=jacobsenColor)
plt.scatter([extDict['PRIA'], extDict['PRIA']], [0, 0], color=priaColor, marker='o', s=dotSize, zorder=2)
plt.text(extDict['PRIA'], .4 / 2, 'SAFE\nPRIA', ha='right', va='center', color=priaColor)
plt.scatter([extDict['FRIA'], extDict['FRIA']], [0, 0], color=friaColor, marker='o', s=dotSize, zorder=3)
plt.text(extDict['FRIA'], .4 / 2, 'SAFE\nFRIA', ha='center', va='center', color=friaColor)
# ax.plot([0,0], [-2,2], color='xkcd:black', zorder=1)

plt.scatter([extDict['PRIA-Star'], extDict['PRIA-Star']], [0, 0], color=priaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['PRIA-Star']+60, .4 / 2, 'SAFE\nPRIA*', ha='right', va='center', color=priaStarColor)
plt.scatter([extDict['FRIA-Star'], extDict['FRIA-Star']], [0, 0], color=friaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['FRIA-Star'], -.6 / 2, 'SAFE\nFRIA*', ha='center', va='center', color=friaStarColor)


plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

xlim = plt.xlim()
plt.plot(bounds, [0,0], color='xkcd:black', zorder=1)

plt.xlim(bounds)
leftXTicks = lax.get_xticks()
newLeftXTicks = leftXTicks[:len(leftXTicks)-1]
lax.set_xticks(newLeftXTicks)
rightTicks, rightLabels = plt.xticks()
plotTicks(ax, rightTicks)

ax.set_xticks([])
lax.set_xticks([])
plt.ylim([-1,1])

plt.yticks([])
plt.suptitle('Fleet Travel Externalities (2021$M/State-Year)\nUnder Estimated Elasticities and Null Hypotheses', y=supTitleHeight)

xlim = plt.xlim()
ylim = plt.ylim()

plt.fill_between([min(xlim), 0], max(ylim), min(ylim), color='xkcd:gray', alpha=.25)
plt.ylim(ylim)
plt.xlim(xlim)

if(sys.platform=='darwin'):
    saveFile='PythonScripts/Plots/Externalities/VMTExternalities.png'
else:
    saveFile = r'PythonScripts\Plots\Externalities\VMTExternalities.png'

plt.savefig(saveFile, bbox_inches='tight', dpi=dpi)

#Raw VMT Plot
fig, (lax, ax) = plt.subplots(1,2,figsize=(x,y), sharey=True, gridspec_kw={'width_ratios':[.15, .85]})
fig, ax = plt.subplots(1,1,figsize=(x,y), sharey=True)
normFactor = 100000000
deltaBars = 1
extDict = getSubExternalityDict(True, True, False)

plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor - deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor - deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

plt.text(extDict['Elast. Upper Bound']/normFactor, 1/2+.1, '95% Confidence\nUpper Bound', ha='center', va='bottom', color='xkcd:green')
plt.text(extDict['Elast. Lower Bound']/normFactor, 1/2+.1, '95% Confidence\nLower Bound', ha='left', va='bottom', color='xkcd:green')
plt.scatter([extDict['Est. Elast.']/normFactor, extDict['Est. Elast.']/normFactor], [0, 0], color='xkcd:dark green', s=dotSize + 20, zorder=3, marker='d')
plt.text(extDict['Est. Elast.']/normFactor - .1, .5 / 2, 'Estimated\nTravel', ha='center', va='center', color='xkcd:dark green')
plt.scatter([extDict['Jacobsen']/normFactor, extDict['Jacobsen']/normFactor], [0, 0], color=jacobsenColor, s=dotSize, zorder=2)
plt.text(extDict['Jacobsen']/normFactor-deltaBars*1.5, -.6 / 2, 'Unit\nElasticity', ha='left', va='center', color=jacobsenColor)
plt.scatter([extDict['PRIA']/normFactor, extDict['PRIA']/normFactor], [0, 0], color=priaColor, marker='o', s=dotSize, zorder=2)
plt.text(extDict['PRIA']/normFactor, .4 / 2, 'SAFE\nPRIA', ha='right', va='center', color=priaColor)
plt.scatter([extDict['FRIA']/normFactor, extDict['FRIA']/normFactor], [0, 0], color=friaColor, marker='o', s=dotSize, zorder=3)
plt.text(extDict['FRIA']/normFactor, .4 / 2, 'SAFE\nFRIA', ha='center', va='center', color=friaColor)


plt.scatter([extDict['PRIA-Star']/normFactor, extDict['PRIA-Star']/normFactor], [0, 0], color=priaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['PRIA-Star']/normFactor+5, .4 / 2, 'SAFE\nPRIA*', ha='right', va='center', color=priaStarColor)
plt.scatter([extDict['FRIA-Star']/normFactor, extDict['FRIA-Star']/normFactor], [0, 0], color=friaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['FRIA-Star']/normFactor, -.6 / 2, 'SAFE\nFRIA*', ha='center', va='center', color=friaStarColor)

plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor + deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor + deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

xlim = plt.xlim()
plt.plot(xlim, [0,0], color='xkcd:black', zorder=1)


plt.xlim(xlim)
leftXTicks = lax.get_xticks()
newLeftXTicks = leftXTicks[:len(leftXTicks)-1]
lax.set_xticks(newLeftXTicks)
rightTicks, rightLabels = plt.xticks()
plotTicks(ax, rightTicks, round=0)

ax.set_xticks([])
lax.set_xticks([])
plt.ylim([-1,1])

plt.yticks([])
plt.suptitle('Fleet Travel (100M Miles/State-Year)\nUnder Estimated Elasticities and Null Hypotheses', y=supTitleHeight)

xlim = plt.xlim()
ylim = plt.ylim()

plt.fill_between([min(xlim), 0], max(ylim), min(ylim), color='xkcd:gray', alpha=.25)
plt.ylim(ylim)
plt.xlim(xlim)

if(sys.platform=='darwin'):
    saveFile='PythonScripts/Plots/Externalities/VMT.png'
else:
    saveFile = r'PythonScripts\Plots\Externalities\VMT.png'

plt.savefig(saveFile, bbox_inches='tight', dpi=dpi)


#Gas Use Plot
fig, (lax, ax) = plt.subplots(1,2,figsize=(x,y), sharey=True, gridspec_kw={'width_ratios':[.15, .85]})
fig, ax = plt.subplots(1,1,figsize=(x,y), sharey=True)
deltaBars = 5

extDict = getSubExternalityDict(True, False)

plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound'], extDict['Elast. Upper Bound'] - deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

plt.text(extDict['Elast. Upper Bound'], 1/2+.1, '95% Confidence\nSet', ha='center', va='bottom', color='xkcd:green')

plt.scatter([extDict['Est. Elast.'], extDict['Est. Elast.']], [0, 0], color='xkcd:dark green', s=dotSize + 20, zorder=3, marker='d')

plt.scatter([extDict['Jacobsen'], extDict['Jacobsen']], [0, 0], color=jacobsenColor, s=dotSize, zorder=2)
plt.text(extDict['Jacobsen'], -.5 / 2, 'Unit\nElasticity', ha='left', va='center', color=jacobsenColor)
plt.scatter([extDict['PRIA'], extDict['PRIA']], [0, 0], color=priaColor, marker='o', s=dotSize, zorder=2)
plt.text(extDict['PRIA'], .4 / 2, 'SAFE\nPRIA', ha='right', va='center', color=priaColor)
plt.scatter([extDict['FRIA'], extDict['FRIA']], [0, 0], color=friaColor, marker='o', s=dotSize, zorder=3)
plt.text(extDict['FRIA'], .4 / 2, 'SAFE\nFRIA', ha='center', va='center', color=friaColor)


plt.scatter([extDict['PRIA-Star'], extDict['PRIA-Star']], [0, 0], color=priaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['PRIA-Star'], -.6 / 2, 'SAFE\nPRIA*', ha='right', va='center', color=priaStarColor)
plt.scatter([extDict['FRIA-Star'], extDict['FRIA-Star']], [0, 0], color=friaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['FRIA-Star']+50, -.6 / 2, 'SAFE\nFRIA*', ha='center', va='center', color=friaStarColor)

plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Upper Bound']], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound']], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound'], extDict['Elast. Lower Bound'] + deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

xlim = plt.xlim()
plt.plot(bounds, [0,0], color='xkcd:black', zorder=1)

plt.xlim(bounds)
leftXTicks = lax.get_xticks()
newLeftXTicks = leftXTicks[:len(leftXTicks)-1]
lax.set_xticks(newLeftXTicks)
rightTicks, rightLabels = plt.xticks()
plotTicks(ax, rightTicks)

ax.set_xticks([])
lax.set_xticks([])
plt.ylim([-1,1])

plt.yticks([])
plt.suptitle('Fleet Highway Gas Use Externalities (2021$M/State-Year)\nUnder Estimated Elasticities and Null Hypotheses', y=supTitleHeight)

xlim = plt.xlim()
ylim = plt.ylim()

plt.fill_between([min(xlim), 0], max(ylim), min(ylim), color='xkcd:gray', alpha=.25)
plt.ylim(ylim)
plt.xlim(xlim)

if(sys.platform=='darwin'):
    saveFile='PythonScripts/Plots/Externalities/GasUseExternalities.png'
else:
    saveFile = r'PythonScripts\Plots\Externalities\GasUseExternalities.png'


plt.savefig(saveFile, bbox_inches='tight', dpi=dpi)

#Raw Gas Use Plot
fig, (lax, ax) = plt.subplots(1,2,figsize=(x,y), sharey=True, gridspec_kw={'width_ratios':[.15, .85]})
fig, ax = plt.subplots(1,1,figsize=(x,y), sharey=True)

extDict = getSubExternalityDict(True, False, False)
normFactor = 100000000
deltaBars = .1
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor - deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Upper Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor - deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

plt.text(extDict['Elast. Upper Bound']/normFactor, 1/2+.1, '95% Confidence\nSet', ha='center', va='bottom', color='xkcd:green')
plt.scatter([extDict['Est. Elast.']/normFactor, extDict['Est. Elast.']/normFactor], [0, 0], color='xkcd:dark green', s=dotSize + 20, zorder=3, marker='d')
plt.text(extDict['Est. Elast.']/normFactor - 10/normFactor, .5 / 2, 'Estimated\nGas Use', ha='center', va='center', color='xkcd:dark green')
plt.scatter([extDict['Jacobsen']/normFactor, extDict['Jacobsen']/normFactor], [0, 0], color=jacobsenColor, s=dotSize, zorder=2)
plt.text(extDict['Jacobsen']/normFactor-deltaBars*1.5, -.6 / 2, 'Unit\nElasticity', ha='left', va='center', color=jacobsenColor)
plt.scatter([extDict['PRIA']/normFactor, extDict['PRIA']/normFactor], [0, 0], color=priaColor, marker='o', s=dotSize, zorder=2)
plt.text(extDict['PRIA']/normFactor+.5, .4 / 2, 'SAFE\nPRIA', ha='right', va='center', color=priaColor)
plt.scatter([extDict['FRIA']/normFactor, extDict['FRIA']/normFactor], [0, 0], color=friaColor, marker='o', s=dotSize, zorder=3)
plt.text(extDict['FRIA']/normFactor, .4 / 2, 'SAFE\nFRIA', ha='center', va='center', color=friaColor)


plt.scatter([extDict['PRIA-Star']/normFactor, extDict['PRIA-Star']/normFactor], [0, 0], color=priaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['PRIA-Star']/normFactor+.5, -.6 / 2, 'SAFE\nPRIA*', ha='right', va='center', color=priaStarColor)
plt.scatter([extDict['FRIA-Star']/normFactor, extDict['FRIA-Star']/normFactor], [0, 0], color=friaStarColor, marker='o', s=dotSize, zorder=5)
plt.text(extDict['FRIA-Star']/normFactor, -.6 / 2, 'SAFE\nFRIA*', ha='center', va='center', color=friaStarColor)

plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Upper Bound']/normFactor], [0, 0], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor], [-1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor + deltaBars], [-1 / 2, -1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)
plt.plot([extDict['Elast. Lower Bound']/normFactor, extDict['Elast. Lower Bound']/normFactor + deltaBars], [1 / 2, 1 / 2], color='xkcd:green', linewidth=lineWidth, zorder=2)

xlim = plt.xlim()
plt.plot(xlim, [0,0], color='xkcd:black', zorder=1)


plt.xlim(xlim)
leftXTicks = lax.get_xticks()
newLeftXTicks = leftXTicks[:len(leftXTicks)-1]
lax.set_xticks(newLeftXTicks)
rightTicks, rightLabels = plt.xticks()
plotTicks(ax, rightTicks)

ax.set_xticks([])
lax.set_xticks([])
plt.ylim([-1,1])

plt.yticks([])
plt.suptitle('Fleet Gas Use (100M Gallons/State-Year)\nUnder Estimated Elasticities and Null Hypotheses', y=supTitleHeight)

xlim = plt.xlim()
ylim = plt.ylim()

plt.fill_between([min(xlim), 0], max(ylim), min(ylim), color='xkcd:gray', alpha=.25)
plt.ylim(ylim)
plt.xlim(xlim)

if(sys.platform=='darwin'):
    saveFile='PythonScripts/Plots/Externalities/GasUse.png'
else:
    saveFile = r'PythonScripts\Plots\Externalities\GasUse.png'

plt.savefig(saveFile, bbox_inches='tight', dpi=dpi)